package serial

import (
	"errors"
	"go.bug.st/serial"
	"strings"
)

// GetRSTypeList 获取RS类型
func GetRSTypeList() []string {
	return []string{"RS485", "RS232"}
}

// GetPortNameList 获取全部串口名称
func GetPortNameList() ([]string, error) {
	return serial.GetPortsList()
}

// GetBaudRateList 获取波特率枚举
func GetBaudRateList() []int {
	return []int{
		50, 75,
		110, 134, 150, 200, 300, 600,
		1200, 1800, 2400, 4800, 7200, 9600,
		14400, 19200, 28800, 38400, 57600, 76800,
		115200, 230400,
	}
}

// dealErr 处理错误
func dealErr(err error, name string) error {
	if err != nil {
		switch err.Error() {
		case "Access is denied.":
			return errors.New("串口无效或被占用")
		case "The system cannot find the file specified.":
			return errors.New("串口未连接:" + name)
		case "serial: timeout":
			return errors.New("串口超时:" + name)
		default:
			if strings.Contains(err.Error(), "no such file or directory") {
				return errors.New("串口未连接:" + name)
			}
		}
	}
	return err
}
